<?php

// $Id$

global $WSC;

require_once('tcms_cia_id_dbc.class.php');


class TCMS_CIA_ID_DBC extends TCMS_CIA implements I_DB_Storable {
	public function is_valid_value($value) {
		if (tangra_is_int($value) && $value > 0) {
			$ret = true;
		} else {
			$ret = false;
		}

		return $ret;
	}


	public function load_by_id(DB_Connection $dbc, $id) {
		tangra_if_not_int_throw_e($id);

		$ret = false;

		$sql = "select ci_type_attr, ".
						"ci, ".
						"value ".
					"from tcms_cia_id where id = $id";
		$rez = $dbc->execute($sql);

		if (!$rez->is_eof()) {
			$rez_obj = $rez->fetch_object();

			$this->set_id($id);
			$this->set_ci_type_attr($rez_obj->CI_TYPE_ATTR);
			$this->set_ci($rez_obj->CI);
			$this->set_value($rez_obj->VALUE);

			$ret = $id;
		}

		return $ret;
	}


	protected function insert(DB_Connection $dbc) {
		$ret = false;

		$id = $dbc->generate_id('tcms_cia_id_seq');
		$ci_type_attr = $this->get_ci_type_attr();
		$ci = $this->get_ci();
		$value = $this->get_value();

		if ($id) {
			$sql = "insert into tcms_cia_id (id, ".
									      "ci_type_attr, ".
									      "ci, ".
									      "value ".
									      ") ".
								"values ".
									      "($id,".
									      "$ci_type_attr, ".
									      "$ci, ".
									      "$value ".
									      ")";
			$dbc->execute($sql);
			$ret = $id;
		} else {
			throw new TE_Exception('tcms_cia_id_seq');
		}

		return $ret;
	}


	protected function update(DB_Connection $dbc) {
		$id = $this->get_id();

		$ci_type_attr = $this->get_ci_type_attr();
		$ci = $this->get_ci();
		$value = $this->get_value();

		$sql = "update tcms_cia_id set ".
						"ci_type_attr = $ci_type_attr, ".
						"ci = $ci, ".
						"value = $value ".
					"where id = ".$id;

		$dbc->execute($sql);
		$ret = $id;

		return $ret;
	}


	public function load_by_ci_thid(DB_Connection $dbc, $ci_id, $attr_type_hid) {
		tangra_if_not_int_throw_e($ci_id);
		$attr_type_hid = addslashes($attr_type_hid);

		$ret = false;
		$sql = "select cia_int.id ".
					"from tcms_cia_id cia_int, ".
							"tcms_ci_type_attrs cita ".
					"where cia_int.ci = $ci_id and ".
							"cia_int.ci_type_attr = cita.id and ".
							"cita.hid = '$attr_type_hid'";
		$rez = $dbc->execute($sql);
		if (!$rez->is_eod()) {
			$rez_obj = $rez->fetch_object();
			$ret = $this->load_by_id($dbc, $rez_obj->ID);
		}

		return $ret;
	}


	public function delete(DB_Connection $dbc, $id) {
		tangra_if_not_int_throw_e($id);
		$sql = "delete from tcms_cia_id where id = $id";
		$rez = $dbc->execute($sql);
	}


	public function get_sql_select_id_ci_for_ci_type_attr($ci_type_attr) {
		tangra_if_not_int_throw_e($ci_type_attr);
		$sql = "select id, ci from tcms_cia_id where ci_type_attr = $ci_type_attr";
		return $sql;
	}


	public function count_all_for_ci_type_attr(DB_Connection $dbc, $ci_type_attr) {
		$sql = "select count(id) as c from tcms_cia_id where ci_type_attr =  $ci_type_attr";
		$rez = $dbc->execute($sql);
		$rez_obj = $rez->fetch_object();

		return $rez_obj->C;
	}


	public function delete_all_for_ci(DB_Connection $dbc, $ci_id) {
		tangra_if_not_int_throw_e($ci_id);
		$sql = "delete from tcms_cia_id where ci = $ci_id";
		$rez = $dbc->execute($sql);
	}
}